//
//  ZFVerifyLoginViewController.m
//  HappilyLoan
//
//  Created by zainishenbian on 2017/3/22.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "ZFVerifyLoginViewController.h"
#import "ZFForgetPswViewController.h"
#import "UIButton+countDown.h"
#import "ZFLoginHttpRequest.h"
#import "ZFHeader.h"

@interface ZFVerifyLoginViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) AlertBoxView *alertV;
@end

@implementation ZFVerifyLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _alertV = [[AlertBoxView alloc] init];
    [self.view addSubview:_alertV];
      [_verifyButton setShadowWidth:2 shadowHeight:2 shadowColor:Main_Color1 radius:10];
    //设置导航栏右侧按钮
    [self setNavgationItem:@"注册"];
}
#pragma mark ———— textfield delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Check for total length
    NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
    
    if (textField == _phoneTF) {
        
        if (proposedNewLength > 11) {
            
            return NO;
        }
    }
    if (textField == _verifyCodeTF) {
        
        if (proposedNewLength > 6) {
            
            return NO;
        }
    }
    
    return YES;
}

#pragma mark ———— button的点击事件
//获取验证码
- (IBAction)getVerifyCode:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    
    if ([ZFLoginConfig isValidatePhone:[ZFPulicAPI replaceBlank:_phoneTF.text]]) {
        
        [button startWithTime:60 title:@"重获验证码" countDownTitle:@"s后重新获取" mainColor:Main_Color1 countColor:[UIColor lightGrayColor]];

        [ZFLoginHttpRequest requestGetSmsCodePhone:[ZFPulicAPI replaceBlank:_phoneTF.text] withCodeType:@"2" sucessBlock:^(NSDictionary *dict) {
            
            if ([dict[@"code"] integerValue] == 1) {
                
                NSLog(@"获取验证码成功");
            }
            else
            {
                NSLog(@"获取验证码失败");
            }
               
        } failureBlock:^(NSInteger failSign) {
             [_alertV appearAndDisappearAlertWithString:@"网络连接失败"];
        }];
    }

}

//验证码登录
- (IBAction)loginClick:(id)sender {
    
    if ([ZFLoginConfig isValidatePhone:[ZFPulicAPI replaceBlank:_phoneTF.text]]) {
        
        if ([ZFPulicAPI replaceBlank:_verifyCodeTF.text]) {
            
            [ZFLoginHttpRequest requestSmsLoginPhone:[ZFPulicAPI replaceBlank:_phoneTF.text] smsCode:[ZFPulicAPI replaceBlank:_verifyCodeTF.text] sucessBlock:^(NSDictionary *dict) {
                
                if ([dict[@"code"] integerValue] == 1) {
                    
                    [self dismissViewControllerAnimated:YES completion:nil];
                    NSLog(@"登录成功");
                }
                else
                {
                    NSLog(@"登录失败");
                }
            } failureBlock:^(NSInteger failSign) {
                
            }];
        }
    }
}
//忘记密码
- (IBAction)forgetPsw:(id)sender {
    
    ZFForgetPswViewController *forgetVC = [ZFForgetPswViewController new];
    [self.navigationController pushViewController:forgetVC animated:YES];
}
//账号密码登录
- (IBAction)accountLogin:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"veriRegister"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"veriRegister"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
